package controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.VCloudAnecdoteReview;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import annotation.Log;
import persistent.factory.DAOFactory;
import util.toolkit.Constant;
import util.toolkit.Expression;
import util.toolkit.ResponseJSON;

/**
 * 后台轶事信息查询
 * @author djf
 * @since 2018/12/10
 */
@Controller
@RequestMapping(value = "/sysanecdotemanagement")
public class SysAnecdoteManagementController {

	/**
	 * 轶事信息查询
	 * @param model
	 * @param req
	 * @param resp
	 * @return
	 * @throws IOException
	 */
	//TODO 轶事信息查询
	@Log(operationType = Constant.OPERTATION_QUERY, operationDescribe = "查询轶事", 
			operationClassPath = "controller.SysAnecdoteManagementController.getAllAnecdote(Model model, HttpServletRequest req,HttpServletResponse resp)", 
			operationMenu = "轶事管理/轶事查看")
	@RequestMapping(value = "/getallanecdote")
	public String getAllAnecdote(Model model, HttpServletRequest req,
			HttpServletResponse resp) throws IOException {

		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("application/json");

		JSONObject result = new JSONObject();
		Expression ex = new Expression();

		try {
			
			String schoolid=req.getParameter("schoolid");
			String topic = req.getParameter("topic");
			String limit = req.getParameter("limit");// 每页条数
			String page = req.getParameter("page");// 当前页

			ex.andEqu("sendStatus", Constant.ALREADY_SENT, Integer.class);
			if(schoolid!=null && !schoolid.equals("")){
				ex.andEqu("sendSchool", schoolid, String.class);
			}
			if (topic != null && !topic.equals("")) {
				ex.andLike("topic", topic, String.class);
			}
			ex.orderByDesc("sendTime");
			String strWhere = ex.toString();
			List<VCloudAnecdoteReview> anList = DAOFactory.getCloudAnecdoteDAO().
					getAnecdotesByStrWhere(strWhere, Integer.parseInt(page), Integer.parseInt(limit));
			List<VCloudAnecdoteReview> listsize = DAOFactory.getCloudAnecdoteDAO().
					getAnecdotesByStrWhere(strWhere);
			if (anList != null && anList.size() > 0) {
				result.put("code", 0);
				result.put("msg", "查询成功！");
				result.put("count", listsize.size());
				JSONArray jsonArray = JSONArray.fromObject(anList);
				result.element("data", jsonArray);
			} else {
				result.put("code", 1);
				result.put("msg", "无查询结果！");
			}
		} catch (Exception e) {
			result.put("code", 2);
			result.put("msg", "出现异常，查询失败，请重试！");
			e.printStackTrace();
		}

		PrintWriter out = resp.getWriter();
		out.println(result);
		return null;
	}

	
	/**
	 * 点击查看待处理轶事控制类
	 * @param model
	 * @param req
	 * @param resp
	 * @return
	 * @throws IOException
	 */
	// TODO 点击查看待处理轶事控制类
	@Log(operationType = Constant.OPERTATION_QUERY, operationDescribe = "查看待处理轶事", 
			operationClassPath = "controller.SysAnecdoteManagementController.getAUntreatedAnecdote(Model model, HttpServletRequest req,HttpServletResponse resp)", 
			operationMenu = "轶事管理/轶事查看")
	@RequestMapping(value = "/getauntreatedanecdote")
	public String getAUntreatedAnecdote(Model model, HttpServletRequest req,
			HttpServletResponse resp) throws IOException {
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("application/json");

		JSONObject result = new JSONObject();
		HttpSession session = req.getSession();
		//Expression exp = new Expression();

		try {
			String aid = req.getParameter("aid");
			VCloudAnecdoteReview cloudanecode = DAOFactory.getCloudAnecdoteDAO().getAnecdoteByid(Integer.parseInt(aid));
			if (cloudanecode != null && !cloudanecode.equals("")) {
				result.put("code", ResponseJSON.FLAG_SUCC);
				result.put("msg", "查询成功！");
				session.removeAttribute("cloudanecode");
				session.setAttribute("cloudanecode", cloudanecode);
			} else {
				result.put("code", ResponseJSON.FLAG_FAIL);
				result.put("msg", "未获取到数据！");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.put("code", ResponseJSON.FLAG_FAIL);
			result.put("msg", "出现异常，查询失败，请重试！");
		}
		PrintWriter out = resp.getWriter();
		out.println(result);
		return null;
	}

	/**
	 * 待处理轶事详情控制类
	 * 
	 * @param model
	 * @param req
	 * @param resp
	 * @return
	 * @throws IOException
	 * @author djf
	 */
	// TODO 待处理轶事详情控制类
	@Log(operationType = Constant.OPERTATION_QUERY, operationDescribe = "查看待处理轶事详情", 
			operationClassPath = "controller.SysAnecdoteManagementController.getUntreatedAnecdoteDetail(Model model,HttpServletRequest req, HttpServletResponse resp)", 
			operationMenu = "轶事管理/轶事详情查看")
	@RequestMapping(value = "/getuntreateanecdotedetail")
	public String getUntreatedAnecdoteDetail(Model model,
			HttpServletRequest req, HttpServletResponse resp) throws IOException {
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("application/json");

		JSONObject result = new JSONObject();
		HttpSession session = req.getSession();

		try {
			VCloudAnecdoteReview cloudanecode = (VCloudAnecdoteReview) session.getAttribute("cloudanecode");
			if (cloudanecode != null) {
				result.put("code", ResponseJSON.FLAG_SUCC);
				result.put("msg", "查询成功！");
				JSONArray jsonArray = JSONArray.fromObject(cloudanecode);
				result.element("data", jsonArray);
			} else {
				result.put("code", ResponseJSON.FLAG_FAIL);
				result.put("msg", "未获取到数据！");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.put("code", ResponseJSON.FLAG_FAIL);
			result.put("msg", "出现异常，数据获取失败，请重试！");
		}
		PrintWriter out = resp.getWriter();
		out.println(result);
		return null;
	}

	/**
	 * 已处理轶事查询
	 * @param model
	 * @param req
	 * @param resp
	 * @return
	 * @throws IOException
	 * @author djf
	 */
	// TODO 已处理轶事查询
	@Log(operationType = Constant.OPERTATION_QUERY, operationDescribe = "查看已处理轶事", 
			operationClassPath = "controller.SysAnecdoteManagementController.getAlreadyHandledAnecdote(Model model,HttpServletRequest req, HttpServletResponse resp)", 
			operationMenu = "轶事管理/轶事查看")
	@RequestMapping(value = "/getalreadyhandledanecdote")
	public String getAlreadyHandledAnecdote(Model model,
			HttpServletRequest req, HttpServletResponse resp) throws IOException {

		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("application/json");

		JSONObject result = new JSONObject();
		HttpSession session = req.getSession();
		Expression exp = new Expression();

		int currentPage = 1;
		int pageSize = 10;

		try {			
			if(req.getParameter("page") != null && !req.getParameter("page").equals("")){
				currentPage = Integer.parseInt(req.getParameter("page"));
			}
			if (req.getParameter("limit") != null && !req.getParameter("limit").equals("")) {
				pageSize = Integer.parseInt(req.getParameter("limit"));
			}		
			String title = req.getParameter("title");
			String schoolid = req.getParameter("schoolid");
			
			
			if (title != null && !title.equals("")) {
				exp.andLike("topic", title, String.class);
			}
			if (schoolid != null && !schoolid.equals("")) {
				exp.andEqu("sendSchool", schoolid, String.class);
			}
			//exp.andEqu("receiveUserid", user.getUserid(), Integer.class);
			exp.andEqu("handlerStatus", Constant.ALREADY_HANDLED, Integer.class);
			exp.orderByDesc("reviewTime");
			String strWhere = exp.toString();
			List<VCloudAnecdoteReview> anList = DAOFactory.getCloudAnecdoteDAO().getAnecdotesByStrWhere(strWhere, currentPage, pageSize);
			List<VCloudAnecdoteReview> listsize = DAOFactory.getCloudAnecdoteDAO().getAnecdotesByStrWhere(strWhere);
			if (anList != null && anList.size() > 0) {
				result.put("code", 0);
				result.put("msg", "查询成功！");
				result.put("count", listsize.size());
				JSONArray jsonArray = JSONArray.fromObject(anList);
				result.element("data", jsonArray);
			} else {
				result.put("code", 1);
				result.put("msg", "无查询结果！");
			}
		} catch (Exception e) {
			result.put("code", 1);
			result.put("msg", "出现异常，查询失败，请重试！");
			e.printStackTrace();
		}

		PrintWriter out = resp.getWriter();
		out.println(result);
		return null;
	}

	
}
